﻿/*
 * @Autor: PK
 * @Date: 2021/12/26 21:07:SS
 */

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using ShkSimulation.core.optimization.extractor;

namespace ShkSimulation.desktop.component.tools {

	public class CellPointDisplay {

		private static int index = 1;
		
		public CellPointDisplay(string tableName, CellPoint point) {
			this.Index = index++;
			this.TableName = tableName;
			this.UniqueKey = point.Name;
			this.RowIndex = point.Row;
			this.ColumnIndex = point.Column;
			this.CurrentValue = point.CurrentValue;
		}

		public int Index { get; private set; }
		public string TableName { get; private set; }
		public string UniqueKey { get; private set; }
		public int RowIndex { get; private set; }
		public int ColumnIndex { get; private set; }
		public string CurrentValue { get; private set; }

	}

	public class CellPointBindingList : BindingList<CellPointDisplay> {

		public Dictionary<string, List<CellPoint>> CellPoints { get; set; }
		
		public CellPointBindingList(DataGridView reporterSelectView, Dictionary<string, List<CellPoint>> points = null) {
			if (points != null && points.Count != 0) {
				foreach (var cell in points) {
					foreach (var cellPoint in cell.Value) {
						Add(new CellPointDisplay(cell.Key, cellPoint));
					}
				}
				this.CellPoints = new Dictionary<string, List<CellPoint>>(points);
			} else {
				this.CellPoints = new Dictionary<string, List<CellPoint>>();
			}
			this.ReporterSelectView = reporterSelectView;
			this.BindView(reporterSelectView);
		}
		
		public void AddRange(Dictionary<string, List<CellPoint>> points) {
			if (points == null || points.Count == 0) {
				return;
			}
			foreach (var cell in points) {
				this.CellPoints.Add(cell.Key, cell.Value);
				foreach (var cellPoint in cell.Value) {
					Add(new CellPointDisplay(cell.Key, cellPoint));
				}
			}
		}
		
		private DataGridView ReporterSelectView { get; set; }

		public void BindView(DataGridView reporterSelectView) {
			reporterSelectView.ResetBindings();
			reporterSelectView.AutoGenerateColumns = true;
			reporterSelectView.DataSource = this;
		}

	}
}